<?php
class App_Admin_Model_Lang extends Main_Db_Table_Abstract
{
	/**
	 * @var string
	 */
	protected $_name = 'lang';
	
	/**
	 * @var id
	 */
	protected $_primary = 'id';
	
	/**
	 * @var array
	 */
	protected $_fields = array(
		'id',
	    'abbr',
	);

	/**
	 * 
	 * @var App_Admin_Model_LangI18n
	 */
	protected $_modelLangI18n;
	
	/**
	 * @return void
	 */
	public function init() 
	{
		$this->_modelLangI18n = Main_Model::factory('Admin_Model_LangI18n');
	}
	
	/**
	 * @param array $data
	 * @return bool
	 */
	public function insertItem($data) 
	{
		$dataOk = $this->beforeSave($data, array(
			'abbr',  
		), array(
			'abbr',
		));
		
		$result = $this->insert($dataOk);
				
		$data['lang_id'] = $this->lastInsertId(); 
		$res1 = $this->_modelLangI18n->insertItem($data);
		
		return $result && $res1;
	}
	
	/**
	 * @param array $data
	 * @param int $id
	 * @return bool
	 */
	public function updateItem($data, $id)
	{
		$id = $this->escape($id);
		$dataOk = $this->beforeSave($data, array(
			'abbr', 
		), array(
			'abbr',
		));
    	
		$res1 = $this->_modelLangI18n->updateItem($data, $id);  	
		$res2 = $this->update($dataOk, "id={$id}");
    	return $res1 && $res2;
	}
	
	/**
	 * @param int $id
	 * @return array
	 */
	public function getItemById($id) 
	{
		$id = $this->escape($id);
		
		$sql = "
			SELECT 
				l.id
				, l.abbr
				, l_i18n.name
			FROM
				lang l
			INNER JOIN
				lang_i18n l_i18n
				ON
					(l.id=l_i18n.lang_id)		
			WHERE
				l.id={$id}		
		";
				
		$list = $this->getAdapter()->fetchRow($sql);
		return $list;
	}
	
	/**
	 * @param string $abbr
	 * @return array
	 */
	public function getItemByAbbr($abbr) 
	{
		$abbr = $this->escape($abbr);
		
		$sql = "
			SELECT 
				l.id
				, l.abbr
				, l_i18n.name
			FROM
				lang l
			INNER JOIN
				lang_i18n l_i18n
				ON
					(l.id=l_i18n.lang_id)		
			WHERE
				l.abbr='{$abbr}'		
		";
				
		$list = $this->getAdapter()->fetchRow($sql);
		return $list;
	}
	
	/**
	 * @return array
	 */
	public function getAll() 
	{
		$sql = "
			SELECT 
				l.id
				, l.abbr
				, l_i18n.name
			FROM
				lang l
			INNER JOIN
				lang_i18n l_i18n
				ON
					(l.id=l_i18n.lang_id)		
		";
				
		$list = $this->getAdapter()->fetchAll($sql);
		
		return $list;
	}
	
	/**
	 * @param int $id
	 * @return bool
	 */
	public function deleteItem($id) 
	{
		$id = $this->escape($id);
		if ($this->_modelLangI18n->deleteItemByLangId($id)) {
			return $this->delete("id={$id}");
		} else {
			return false;
		}	
	}

	/**
	 * @param string $langAbbr
	 * @return string
	 */
	public function getLangIdByAbbr($langAbbr) 
	{
		$langAbbr = $this->escape($langAbbr);
		
		$sql = "
			SELECT
				id
			FROM
				lang
			WHERE
				abbr='{$langAbbr}'						
		";
		
		$res = $this->getAdapter()->fetchOne($sql);
				
		return $res;		
	}
	
	/**
	 * @deprecated
	 * @return array
	 */
	/*public function getLangAbbrs()
	{
		$res = array();
		
		$sql = "
			SELECT
				id, abbr
			FROM
				lang
		";

		$list = $this->getAdapter()->fetchAll($sql);
		if (!empty($list)) {
			foreach ($list as $lang) {
				$res[$lang['id']] = $lang['abbr']; 
			}	
		}
		
		return $res;		
	}*/
}
?>